<?php
namespace app\shop\controller;

use \think\Controller;
use \think\Db;
class Notice extends Base
{


    /**
     * 消息通知列表:notice
     * type $page
     */
    public function notice(){
        header("Access-Control-Allow-Origin:*");
        extract(input());
//        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
//        if($auth == false){
//            $returnData['status'] = 301;
//            $returnData['msg'] = "Token失效！请重新登录！";
//            return json_encode($returnData);
//        }
        //不限时间了，客户说的
//        $time = Db::table('up_shops')
//            ->where('s_id',$user_id)->value('s_time');
        if($type == 1){
            $data = Db::table('up_notice')
                ->where('n_type',1)
                ->where('n_app',$app)
                ->where('n_delete','not like','%,'.$user_id.',%')
//                ->where('n_time','>=',$time)
                ->order('n_top DESC,n_time DESC')
                ->page($page,15)
                ->select();
            if($data){
                foreach ($data as $k=>$v){
                    $data[$k]['read'] = 0;
                    if(strpos($v['n_read'],",$user_id,") !== false){
                        $data[$k]['read'] = 1;
                    }
                }
            }
        }else{
            $data = Db::table('up_notice')
                ->where('n_type',2)
                ->where('n_user_id',$user_id)
//                ->where('n_time','>=',$time)
                ->order('n_time DESC')
                ->page($page,15)
                ->select();
        }
        return json_encode(['status'=>200,'msg'=>$data]);
    }

    /**
     * 消息通知详情
     * n_id
     */
    public function noticeInfo(){
        header("Access-Control-Allow-Origin:*");
        extract(input());
//        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
//        if($auth == false){
//            $returnData['status'] = 301;
//            $returnData['msg'] = "Token失效！请重新登录！";
//            return json_encode($returnData);
//        }
        $notice = Db::table('up_notice')->where('n_id',$n_id)->find();
        $type = $notice['n_type'];
        if($type == 1){
            $read = $notice['n_read'];
            if(strpos($read,",$user_id,") !== false){
                $read = 1;
            }else{
                $read = $read . $user_id . ',';
                Db::table('up_notice')->where('n_id',$n_id)
                    ->update(['n_read'=>$read]);
            }
        }else{
            Db::table('up_notice')->where('n_id',$n_id)
                ->update(['n_read'=>1]);
        }
        return json_encode(['status'=>200,'msg'=>$notice]);
    }
    /**
     * 删除某个消息通知:delNotice
     * n_id
     */
    public function delNotice(){
        header("Access-Control-Allow-Origin:*");
        extract(input());
//        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
//        if($auth == false){
//            $returnData['status'] = 301;
//            $returnData['msg'] = "Token失效！请重新登录！";
//            return json_encode($returnData);
//        }
        $notice = Db::table('up_notice')->where('n_id',$n_id)->find();
        $type = $notice['n_type'];
        if($type == 1){
            $del = $notice['n_delete'];
            $del = $del . $user_id . ',';
            $res = Db::table('up_notice')->where('n_id',$n_id)
                ->update(['n_delete'=>$del]);
        }else{
            $res = Db::table('up_notice')->where('n_id',$n_id)
                ->delete();
        }

        if($res){
            return json_encode(['status'=>200,'msg'=>'成功']);
        }else{
            return json_encode(['status'=>0,'msg'=>'删除失败']);
        }
    }
    /**
     * 意见反馈：feedback
     * $content,$phone,$type
     */
    public function feedback(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = array();
        $data['f_user_id'] = $user_id;
//        $data['f_type'] = $type;
        $data['f_content'] = $content;
//        $data['f_phone'] = $phone;
        $data['f_time'] = date('Y-m-d H:i:s');
        $data['f_where'] = 2;
        $res = Db::table('up_feedback')->insert($data);
        if($res){
            return json_encode(['status'=>200,'msg'=>'提交成功']);
        }else{
            return json_encode(['status'=>0,'msg'=>'提交失败']);
        }
    }

    /**
     * 未送达扣分
     * 定时器一！！！！！！！！！！！！！！！！！！！！！
     * 每分钟执行
     */
    public function minuteChangeJifen()
    {
        header("Access-Control-Allow-Origin:*");
        set_time_limit(0);
        $where = array();
        $where['order_status'] = 2;//公共条件
        $where['order_send_status'] = array('<',3);//公共条件
        $where['order_driver_status'] = array('<',3);//公共条件
        $order = Db::table('up_order')
            ->where($where)
            ->field('order_shopid,order_over,order_paytime,order_id')
            ->select();
        $nowtime = time();
        if($order){
            foreach ($order as $k=>$v){
                $sid = $v['order_shopid'];
                $over = $v['order_over'];
                $userid = $v['order_shopid'];
                $paytime = strtotime($v['order_paytime']);
                $value = ($nowtime-$paytime)/60;//相差多少分钟
                if($value > 40 && $over < 1){
                    $this->minuteChangeJifenForShop($sid,$userid,$v['order_id'],40,1);
                }
                if($value > 50 && $over < 2){
                    $this->minuteChangeJifenForShop($sid,$userid,$v['order_id'],50,2);
                }
                if($value > 60 && $over < 3){
                    $this->minuteChangeJifenForShop($sid,$userid,$v['order_id'],60,3);
                }
                if($value > 70 && $over < 4){
                    $this->minuteChangeJifenForShop($sid,$userid,$v['order_id'],70,4);
                }
                if($value > 80 && $over < 5){
                    $this->minuteChangeJifenForShop($sid,$userid,$v['order_id'],80,5);
                }
                if($value > 90 && $over < 6){
                    $this->minuteChangeJifenForShop($sid,$userid,$v['order_id'],90,6);
                }
            }
        }
        //2、超过3小时的未收货的默认收货
        $today = date('Y-m-d');
        $two_hours_ago = date("Y-m-d",(strtotime("{$today}")-3600*3));
        Db::table('up_order')
            ->where(function ($query) use ($two_hours_ago) {
                $query->where('order_status',2)->where('order_send_status',3)->where('order_in_out',1)
                    ->where('order_last_time','<=',$two_hours_ago);
            })
            ->whereOr(function ($query) use ($two_hours_ago) {
                $query->where('order_status',2)->where('order_driver_status',3)->where('order_in_out',1)
                    ->where('order_last_time','<=',$two_hours_ago);
            })
            ->update(['order_status'=>3]);
        //2、超过24小时的不评论的默认好评
        $hours_ago_24 = date("Y-m-d",(strtotime("{$today}")-3600*24));
        $pls = Db::table('up_order')
            ->where('order_status',3)
            ->where('order_in_out',1)
            ->where('order_last_time','<=',$hours_ago_24)
            ->field('order_id,order_userid,order_shopid')
            ->select();
        if($pls){
            foreach ($pls as $kk=>$vv){
                $order_id = $vv['order_id'];
                $user_id = $vv['order_userid'];
                $shid = $vv['order_shopid'];
                $have = Db::table('up_good_comment')->where('comment_order_id',$order_id)
                    ->field('comment_id')->find();
                if(!$have){//计算正式开始
                    Db::table('up_order')->where('order_id',$order_id)->update(['order_status'=>4]);
                    $user = Db::table('up_u_user')->where('user_id',$user_id)
                        ->field('user_username,user_image')->find();
                    $goodids = Db::table('up_order_info')
                        ->where('oi_order_id',$order_id)
                        ->column('oi_good_id');
                    $str = implode($goodids,',');
                    $comment = array();
                    $comment['comment_goodid'] = ','.$str.',';
                    $comment['comment_shopid'] = $shid;
                    $comment['comment_user_id'] = $user_id;
                    $comment['comment_score'] = 5;
                    $comment['comment_content'] = '非常满意';
                    $comment['comment_time'] = date('Y-m-d H:i:s');
                    $comment['comment_order_id'] = $order_id;
                    $comment['comment_username'] = $user['user_username'];
                    $comment['comment_image'] = $user['user_image'];
                    Db::table('up_good_comment')->insert($comment);
                    //1、商品的评分和人数修改
                    foreach ($goodids as $v){
                        $scoredata = Db::table('up_goods')->where('g_id',$v)->field('g_num,g_score')->find();
                        $newscore = ($scoredata['g_num']*$scoredata['g_score']+5)/($scoredata['g_num']+1);
                        $goodscore = round($newscore,1);
                        Db::table('up_goods')->where('g_id',$v)->update(['g_score'=>$goodscore]);
                    }
                    Db::table('up_goods')->where('g_id','in',$goodids)->setInc('g_num');
                    //2、店铺的评分
                    $shopnum = Db::table('up_good_comment')->where('comment_shopid',$shid)->count();
                    $shopscore = Db::table('up_shops')->where('s_id',$shid)->value('s_score');
                    $shopnewscroe = (($shopnum-1)*$shopscore+5)/$shopnum;
                    $nowscore = round($shopnewscroe,1);
                    Db::table('up_shops')->where('s_id',$shid)->update(['s_score'=>$nowscore]);
                }
            }
        }
    }
    public function minuteChangeJifenForShop($sid,$userid,$order_id,$minute,$over)
    {
        //1、商户减分
        Db::table('up_shops')->where('s_id',$sid)->setDec('s_integral',1);
        $nowfen = Db::table('up_shops')->where('s_id',$sid)->value('s_integral');
        allInNotice('扣分通知','您有订单'.$minute.'分钟未送达，扣一分',$userid);
        allInJifen($minute.'分钟未送达',2,1,$userid);
        if($nowfen <= 0){
            Db::table('up_shops')->where('s_id',$sid)->update(['s_status'=>2]);
        }
        //2、改订单扣分分钟
        Db::table('up_order')->where('order_id',$order_id)->update(['order_over'=>$over]);
    }
    /**
     * 关店扣分
     * 定时器二！！！！！！！！！！！！！！！！！！！！！
     * 每天执行
     */
    public function dayChangeJifen()
    {
        header("Access-Control-Allow-Origin:*");
        set_time_limit(0);
        $where = array();
        $where['s_admin'] = 1;//公共条件
        $where['s_status'] = array('<>',1);//公共条件
        $order = Db::table('up_shops')
            ->where($where)
            ->select();
        $nowtime = time();
        if($order){
            foreach ($order as $k=>$v){
                $sid = $v['s_id'];
                $userid = $v['s_id'];
                $paytime = strtotime($v['s_clicktime']);
                $value = ($nowtime-$paytime)/3600/24;//相差多少天
                if($value > 7){
                    //1、商户减分
                    Db::table('up_shops')->where('s_id',$sid)->setDec('s_integral',3);
                    $nowfen = Db::table('up_shops')->where('s_id',$sid)->value('s_integral');
                    allInNotice('扣分通知','您已关店超过7天，扣三分',$userid);
                    allInJifen('关店超过7天',2,3,$userid);
                    if($nowfen <= 0){
                        Db::table('up_shops')->where('s_id',$sid)->update(['s_status'=>2]);
                    }
                }
            }
        }
    }
}